<template>
  <div class="mod-sys__dept">
    <el-form :model="state.dataForm" label-position="top" @keyup.enter="state.getDataList()">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="模糊搜索">
            <el-input v-model="state.dataForm.keywords" placeholder="请输入" clearable></el-input>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="6">
          <el-form-item label="年级">
            <el-select v-model="state.dataForm.gradeId" clearable placeholder="请选择">
              <el-option :value="item.id" :label="item.name" v-for="(item, index) in gradeList"
                :key="index"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="班级">
            <el-select v-model="state.dataForm.classId" clearable placeholder="请选择">
              <el-option :value="item.id" :label="item.name" v-for="(item, index) in state.dataList"
                :key="index"></el-option>
            </el-select>
          </el-form-item>
        </el-col> -->
      </el-row>
      <el-row justify="end">
        <el-form-item>
          <el-button @click="state.getDataList()">查询</el-button>
        </el-form-item>
        <el-form-item style="margin-left: 20px">
          <el-button v-if="state.hasPermission('study:grade:save')" type="primary"
            @click="addOrUpdateHandle()">新增</el-button>
        </el-form-item>
        <el-form-item style="margin-left: 20px;">
          <el-button v-if="state.hasPermission('study:student:save')" type="primary"
            @click="leadingInHandle()">导入</el-button>
        </el-form-item>
      </el-row>
    </el-form>
    <div style="display: flex;">
      <!-- <div class="grade-tree">
        <div class="grade-tree-header">
          <span>年级</span>
          <el-button type="primary" link @click="state.getDataList()">新增</el-button>
        </div>
        <div class="grade-tree-item" v-for="(item, index) in gradeList" :key="index">
          <span>{{ item.name }}</span>
          <el-button type="danger" link @click="state.getDataList()">删除</el-button>
        </div>
      </div> -->
      <el-table v-loading="state.dataListLoading" :data="state.dataList" row-key="id" border style="width: 100%">
        <el-table-column prop="gradeName" label="年级" header-align="center">
          <template v-slot="scope">
            {{ getGradeName(scope.row.gradeId) }}
          </template>
        </el-table-column>
        <el-table-column prop="name" label="班级" header-align="center"></el-table-column>
        <el-table-column prop="owner" label="负责人" header-align="center"></el-table-column>
        <el-table-column prop="ownerTel" label="联系方式" header-align="center"></el-table-column>
        <el-table-column label="操作" fixed="right" header-align="center" align="center">
          <template v-slot="scope">
            <el-button v-if="state.hasPermission('study:class:update')" type="primary" link
              @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
            <el-button v-if="state.hasPermission('study:class:delete')" type="primary" link
              @click="state.deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 导入 -->
    <leading-in ref="leadingInRef" @refreshDataList="state.getDataList"></leading-in>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref, toRefs } from "vue";
import useView from "@/hooks/useView";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";

const router = useRouter();
const view = reactive({
  getDataListURL: "/study/class/page",
  getDataListIsPage: true,
  deleteURL: "/study/class",
  deleteIsBatch: true,
  dataForm: {
    keywords: "",
    gradeId: "",
    classId: "",
  }
});

const gradeList = ref<any>([]);

const state = reactive({ ...useView(view), ...toRefs(view) });

// 获取年级
const getGradeList = () => {
  return baseService.get("/study/grade/page").then((res) => {
    if (res.code !== 0) {
      return ElMessage.error(res.msg);
    }
    gradeList.value = res.data.list;
  });
};
const getGradeName = (id: string) => gradeList.value.find((d: any) => d.id === id)?.name;

const addOrUpdateHandle = (id?: number) => {
  router.push({
    path: '/study/class/edit',
    query: {
      id
    },
  });
};

const leadingInRef = ref();
const leadingInHandle = () => {
  leadingInRef.value.init({
    title: '年级班级',
    templateFile: '/src/assets/file/class-template.xlsx',
    uploadPath: '/study/class/import',
    templatePath: '/study/class/getTemplate',
  });
};

const append = (data: any) => { };
const remove = (data: any, node: any) => { };

onMounted(() => {
  getGradeList();
})
</script>

<style lang="less" scoped>
.grade-tree {
  width: 200px;
  height: calc(100vh - 280px);
  background-color: #fff;
  border-radius: 10px;
  margin-right: 20px;

  &-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 40px;
    line-height: 40px;
    font-weight: bold;
    border-bottom: 1px solid #eee;
    padding: 0 20px;
  }

  &-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 40px;
    line-height: 40px;
    border-bottom: 1px solid #eee;
    padding: 0 20px;
  }
}
</style>
